package TUILayer;
import ControlLayer.*;
import java.util.Scanner;



public class LoanUI
{
    private LoanCtr LoanCtr;//Let LoanUI know the controller 
    
    public LoanUI()
    {
        //Constructor.
        LoanCtr = LoanCtr.getInstance();
    }
    
    public void start()
    {
        //Starts the loan menu.
        LoanMenu();
    }
    
    public void LoanMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice = writeLoanMenu();
            switch(choice)
            {
                case 1:
                    findLoan();
                    break;
                case 2:
                    CreateLoan();
                    break;
                case 3:
                    modifyLoan();
                    break;
                case 4:
                    listAllLoans();
                    break;
                case 5:
                    // Do nothing go to default
                default:
                    exit = true;
                    break;
            }
        }
    }
    
    private int writeLoanMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Loan Menu ***");
            System.out.println(" (1) Show Loan Information");
            System.out.println(" (2) Create loan");
            System.out.println(" (3) Mark loan as 'Recieved'");
            System.out.println(" (4) Show all Loans");
            System.out.println(" (5) Return");
            int choise = keyboard.nextInt();
            return choise;
    }
    
    public long inputID(String typeOfID)
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write " + typeOfID + ":  ");
           try{
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You need to write a number - try again.");
           }
        }//end while
        return id;
    }
    
    private String inputDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write loan date: " );
        String Date = keyboard.nextLine();
        return Date;
    }
    
    private String inputPeriod()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write loan period: " );
        String period = keyboard.nextLine();
        return period;
    }
    
    private String inputSerialNumber()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Write Copy's Serial Number: " );
        String periode = keyboard.nextLine();
        return periode;
    }
  
    private void findLoan()
    {     
        //Searches for a certain loan.
        long id = inputID("loan");
        String str = LoanCtr.printLoan(LoanCtr.findLoan(id));
        System.out.print(str);
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\nPress 'Enter' to return.");
        String input = keyboard.nextLine();
    }
    
    private void CreateLoan()
    {
        //Creates a loan.
        long id = inputID("Loan ID");
        String Date = inputDate();
        String periode = inputPeriod();
        long pID = inputID("Persons ID");
        String serialNumber = inputSerialNumber();
        String str = LoanCtr.createLoan(pID, serialNumber, id, Date, periode);
        System.out.println(str);
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\nPress 'Enter' to return.");
        String input = keyboard.nextLine();
    }
    
    private void modifyLoan()
    {
        //Modifies the information about a loan.
        String serialNumber = inputSerialNumber();
        LoanCtr.returnCopy(serialNumber);
    }
    
    private void listAllLoans()
    {
        //Prints a list of all loaned dvds.
        String Loan = LoanCtr.listAllLoans();
        System.out.print(Loan);
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\nPress 'Enter' to return.");
        String input = keyboard.nextLine();
    }
}
